package com.android.nextBus.Activities;

import com.android.nextBus.DB.SQLiteOperationsWrapper;
import com.android.nextBus.Data.CredentialsItem;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


/**
 * This Activity is responsible getting the account information 
 * from the user. The layout for this activity is the create an 
 * account screen.
 *TODO add funcinality to fields and error checking
 *
 */
public class AccountCreationActivity extends Activity {

	private EditText nameEntry,
	emailEntry,
	countryEntry,
	addressEntry,
	zipCodeEntry,
	passWordEntry,
	verifyPassWordEntry,
	busPassEntry;
	
	private TextView textHeader;
	private SQLiteOperationsWrapper db;
	private CredentialsItem credentials;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.create_an_account_screen);
		
		db = new SQLiteOperationsWrapper(this);
		db.open();
		
		credentials = db.getCredentials();
		
		db.close();
		

		if(credentials != null) {
			
			//If credentials already exist we change the heading 
			//to say update information
			//and put the existing information in the appropriate fields 
			textHeader =(TextView)findViewById(R.id.textCreateAnAccountHeader);
			textHeader.setText("Update your account Information");
			nameEntry = (EditText)findViewById(R.id.editText1);
			emailEntry = (EditText)findViewById(R.id.editText2);
			countryEntry = (EditText)findViewById(R.id.editText3);
			addressEntry = (EditText)findViewById(R.id.editText4);
		
			zipCodeEntry= (EditText)findViewById(R.id.editText5);
			passWordEntry = (EditText)findViewById(R.id.editText6);
			verifyPassWordEntry = (EditText)findViewById(R.id.editText7);
			busPassEntry = (EditText)findViewById(R.id.editText8);
			
			nameEntry.setText(credentials.getName());
			emailEntry.setText(credentials.getEmail());
			countryEntry.setText(credentials.getCountry());
			addressEntry.setText(credentials.getAddress());
			zipCodeEntry.setText("" + credentials.getZipcode());
			passWordEntry.setText(credentials.getPassword());
			verifyPassWordEntry.setText(credentials.getPassword());
			busPassEntry.setText(credentials.getBusPassNumber());
			
		} else {
		
		
		 	nameEntry = (EditText)findViewById(R.id.editText1);
			emailEntry = (EditText)findViewById(R.id.editText2);
			countryEntry = (EditText)findViewById(R.id.editText3);
			addressEntry = (EditText)findViewById(R.id.editText4);
			zipCodeEntry= (EditText)findViewById(R.id.editText5);
			passWordEntry = (EditText)findViewById(R.id.editText6);
			verifyPassWordEntry = (EditText)findViewById(R.id.editText7);
			busPassEntry = (EditText)findViewById(R.id.editText8);
		}
		
	}
	
	
//	public void signUpLaterButtonOnClick(View v){
//		Intent intent = new Intent(this, PaymentMethodActivity.class);
//		startActivity(intent);
//	}
	
	
	public void signUpOnClick(View v){
		boolean errorHappened = false;
		
		if(nameEntry.getText().toString().length() < 3){
			errorHappened = true;
			nameEntry.setError("Name must be atleast 3 letters long");
			nameEntry.setText("");

			nameEntry.setHint("Invalid!");
		}
		
		
		if(nameEntry.getText().toString().length() > 30){
			errorHappened = true;
			nameEntry.setError("Name max is 30 in length.");
			nameEntry.setText("");

			nameEntry.setHint("Invalid!");
		}
		
		if(!emailEntry.getText().toString().matches("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+")){
			emailEntry.setError("Invald email format, please try again");
			emailEntry.setHint("Invalid!");
			emailEntry.setText("");

			errorHappened = true;
		}
		
	
		if(emailEntry.getText().toString().length() > 30){
			errorHappened = true;
			emailEntry.setError("Email address max is 30 in length.");
			emailEntry.setText("");
			emailEntry.setHint("Invalid!");
		}
		
		if(countryEntry.getText().toString().length() < 2){
			errorHappened = true;
			countryEntry.setError("Please enter a  country");
			countryEntry.setText("");

			countryEntry.setHint("Invalid!");

		}
		
		if(addressEntry.getText().toString().equals("")){
			errorHappened = true;
			addressEntry.setError("Please enter an address");
			addressEntry.setText("");

			addressEntry.setHint("Invalid!");

		}
		
		
		if(addressEntry.getText().toString().length() > 30){
			errorHappened = true;
			addressEntry.setError("Address max is greater than 30 in length.");
			addressEntry.setText("");
			addressEntry.setHint("Invalid!");
		}
		
		
		if((zipCodeEntry.getText().toString().length() < 5) || (!zipCodeEntry.getText().toString().matches("[0-9]+"))){
			errorHappened = true;
			zipCodeEntry.setError("Zip code must be atleast 5 numbers long");
			zipCodeEntry.setText("");

			zipCodeEntry.setHint("Invalid!");

		}
		
		if(passWordEntry.getText().toString().length() < 6){
			errorHappened = true;
			passWordEntry.setError("Password must be atleast 6 characters long");
			passWordEntry.setText("");

			passWordEntry.setHint("Invalid!");

		}
		
		if(!passWordEntry.getText().toString().equals(verifyPassWordEntry.getText().toString())){
			errorHappened = true;
			passWordEntry.setError("Password and Verify Password do not match");
			verifyPassWordEntry.setText("");
			passWordEntry.setHint("Invalid!");

		}
		
		if((busPassEntry.getText().toString().length() < 16) || (!busPassEntry.getText().toString().matches("[0-9]+"))){
			errorHappened = true;
			busPassEntry.setError("Bus Pass Number must be at least 16 numbers long");
			busPassEntry.setText("");

			busPassEntry.setHint("Invalid!");

		}
		
		if(!errorHappened){
			
			// insert something to check if this is updating or initial input
			db.open();
						
			db.insertCredentials(passWordEntry.getText().toString(), nameEntry.getText().toString(), emailEntry.getText().toString(),
					countryEntry.getText().toString(), addressEntry.getText().toString(), Integer.parseInt(zipCodeEntry.getText().toString()),
					busPassEntry.getText().toString());
					    
			db.close();
		    
			
			Toast.makeText(this, R.string.thank_you, Toast.LENGTH_LONG).show();
		
			if(this.getIntent().getExtras() !=null){
			if(this.getIntent().getExtras().getBoolean("refillPass"))
			{
			Intent intent = new Intent(this, RefillBusPassActivity.class);
			startActivity(intent);
			}
			}
			finish();  // return to last activity, so we can go back to main menu or to paypal page
		}

	}
}
